#include <stdio.h>

int main() {
    int m, n;
    scanf("%d %d", &m, &n);
    int a = m % n;
    int bb[n][n];
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d", &bb[i][j]);
        }
    }
    
    for (int i = 0; i < n; i++) {
        int cc = n - a;
        for (int j = cc; j < n; j++) {
            printf("%d ", bb[i][j]);
        }
        for (int j = 0; j < cc; j++) {
            printf("%d ", bb[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}